﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Measures;
using DiscoveryLogic.Analytics.DataObjects.Visuals;

namespace DiscoveryLogic.Analytics.Modules.Nodes
{
    public class NodeHelper
    {
        public static BitmapImage CreateImage(
            VerticeVisual visual, VerticeMeasure measure, 
            VerticeMeasureRange range, bool selected)
        {

            
            string imgFilePath = "/" + EntityIcons.ImageFolder + "/" + visual.Icon;
            if (selected)
                imgFilePath = "/" + EntityIcons.ImageFolder + "/" + visual.SelectedIcon;
            BitmapImage img = new BitmapImage();
            Uri imgUri = new Uri(imgFilePath, UriKind.Relative);
            
            img.BeginInit();
            img.UriSource = imgUri;
            img.EndInit();
            
            //Image icon=new Image();
            //icon.Source = img;
            //icon.SetValue(Image.WidthProperty,size.Width);
            //icon.SetValue(Image.HeightProperty,size.Height);
            //return icon;
            return img;
        }

        public static Shape CreateShape(
            VerticeVisual visual, VerticeMeasure measure, string flag,
            VerticeMeasureRange range, bool selected)
        {
            Size size = GetScaledSize(visual, measure, range);

            if (selected)
            {
                return NodeShapeHelper.CreateShape(visual.Shape, size, visual.SelectedColor);
            }
            else if (!string.IsNullOrEmpty(flag))
            {
                Color color = visual.Color;
                if (visual.FlagColors != null && visual.FlagColors.ContainsKey(flag))
                    color = visual.FlagColors[flag];
                return NodeShapeHelper.CreateShape(visual.Shape, size, color);
            }
            else
            {
                return NodeShapeHelper.CreateShape(visual.Shape, size, visual.Color);
            }
        }

        public static Size GetScaledSize(
            VerticeVisual visual, VerticeMeasure measure,
            VerticeMeasureRange range)
        {
            double minSize = double.Parse(ConfigurationManager.AppSettings.Get("VerticeMinSize"));
            double maxSize = double.Parse(ConfigurationManager.AppSettings.Get("VerticeMaxSize"));
            Size size = visual.Size;
            if (visual.SizeMeasure != DynamicVerticeSizeMeasure.Fixed)
            {
                switch (visual.SizeMeasure)
                {
                    case DynamicVerticeSizeMeasure.BetweenCentrality:
                        double w1 = VerticeMeasureRange.LinearScale(minSize, maxSize, range.BetweenCentralityRange,
                                                                   measure.BetweenCentrality);
                        size = new Size(w1, w1);
                        break;
                    case DynamicVerticeSizeMeasure.CloseCentrality:
                        double w2 = VerticeMeasureRange.LinearScale(minSize, maxSize, range.CloseCentralityRange,
                                                                   measure.CloseCentrality);
                        size = new Size(w2, w2);
                        break;
                    case DynamicVerticeSizeMeasure.Degree:
                        double w3 = VerticeMeasureRange.LinearScale(minSize, maxSize, range.DegreeRange, measure.Degree);
                        size = new Size(w3, w3);
                        break;
                    case DynamicVerticeSizeMeasure.InDegree:
                        double w4 = VerticeMeasureRange.LinearScale(minSize, maxSize, range.InDegreeRange, measure.InDegree);
                        size = new Size(w4, w4);
                        break;
                    case DynamicVerticeSizeMeasure.OutDegree:
                        double w5 = VerticeMeasureRange.LinearScale(minSize, maxSize, range.OutDegreeRange, measure.OutDegree);
                        size = new Size(w5, w5);
                        break;
                }
            }
            return size;
        }

        public static BitmapImage CreateImage(string imgFileName)
        {
            string imgFilePath = "/" + EntityIcons.ImageFolder + "/" + imgFileName;
            BitmapImage img = new BitmapImage();
            Uri imgUri = new Uri(imgFilePath, UriKind.Relative);

            img.BeginInit();
            img.UriSource = imgUri;
            img.EndInit();

           
            return img;
        }

        public static BitmapImage CreateImage(FileInfo imgFile)
        {
            BitmapImage img = new BitmapImage();
            Uri imgUri = new Uri(imgFile.FullName, UriKind.Absolute);

            img.BeginInit();
            img.UriSource = imgUri;
            img.EndInit();

            return img;
        }
    }
}
